// create by IAmFiveHigh on 2024/7/19

class LinkListQueue<T> {

    private var size: Int = 0
    private var head: LinkListNode<T>? = null
    private var tail: LinkListNode<T>? = null

    fun getSize(): Int {
        return  this.size
    }

    fun isEmpty(): Boolean {
        return this.size == 0
    }

    fun enqueue(e: T) {
        val node = LinkListNode<T>(e)
        if (this.isEmpty()) {
            this.head = node
            this.tail = node
        }else {
            this.tail?.next = node
            this.tail = node
        }
        this.size += 1
    }

    fun dequeue(): T? {
        if (isEmpty()) {
            return null
        }

        val node = this.head
        if (this.size == 1) {
            this.head = null
            this.tail = null
        }else {
            this.head = this.head?.next
            node?.next = null
        }
        this.size -= 1
        return node?.e
    }

    /// 返回链表内部的元素遍历
    fun traversalList(): List<T> {
        var pre: LinkListNode<T>? = this.head
        val resultList = mutableListOf<T>()
        while (pre != null) {
            pre.e?.let {
                resultList.add(it)
            }
            pre = pre.next
        }
        return resultList.toList()
    }
}